package com.traffic.controller.trafficline;


import com.ainsurtech.proxy.Response;
import com.ainsurtech.proxy.demo.TrafficLinePage;
import com.traffic.entity.TrafficLine;
import com.traffic.trafficLine.TrafficLineService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2018/4/6.
 */
@RestController
@RequestMapping("/trafficline/")
public class TraffiicLineController {

    @Autowired
    private TrafficLineService service;


    /**
     * 添加对象
     * @param dto 对象
     * @return
     * @ApiOperation为添加的API相关注解
     *
     */
    @ApiOperation(value = "样例添加",httpMethod = "POST", notes = "暂无")
    @PostMapping("add")
    public Response<Void> add(@ApiParam(required = true, name = "dto", value = "添加对象信息") @RequestBody TrafficLineDTO dto) {
        TrafficLine entity = new TrafficLine();
        BeanUtils.copyProperties(dto,entity);
        this.service.addTrafficLine(entity);
        return new Response<>();
    }

    /**
     * 更新对象
     *
     * @param dto 更新对象
     * @return
     */
    @ApiOperation(value = "样例更新",httpMethod = "PUT", notes = "暂无")

    @PostMapping("update")
    public Response<Void> update(@RequestBody TrafficLineDTO dto) {
        TrafficLine entity = service.get(dto.getId());

        BeanUtils.copyProperties(dto,entity);
        this.service.updateTrafficLine(entity);
        return new Response<>();
    }

    /**
     * 删除对象
     *
     * @return
     */
    @ApiOperation(value = "样例删除",httpMethod = "POST", notes = "暂无")
    @PostMapping("del")
    public Response<Void> delete(@RequestBody TrafficLineDTO dto) {
        this.service.delTrafficLine(dto.getId());
        return new Response<>();
    }


    /**
     * 查询列表(分页)
     * @param page 分页对象
     * @param line 线路
     * @return
     */
   @ApiOperation(value = "样例列表",httpMethod = "GET", notes = "暂无")
    @GetMapping("findAll")
    public Response<Page<TrafficLinePage>> list(@ApiParam(required = true, name = "page", value = "分页对象") @PageableDefault Pageable page,
                                                @ApiParam(required = false, name = "name", value = "姓名") @RequestParam(required = false) String line) {

       Page<TrafficLine> pages = service.findTrafficLine(page);
        Page<TrafficLinePage> respS = pages.map(new Converter<TrafficLine, TrafficLinePage>() {
            public TrafficLinePage convert(TrafficLine source) {
                TrafficLinePage resp = new TrafficLinePage();
                if (source != null) {
                    BeanUtils.copyProperties(source, resp);
                }
                return resp;
            }
        });
        return new Response<>(respS);
    }

    /**
     * 查询所有的线路信息
     * @return
     */
    @ApiOperation(value = "样例列表",httpMethod = "GET", notes = "暂无")
    @GetMapping("findAll2")
    public List<TrafficLine> list2() {

        List<TrafficLine> pages = service.findAll();

        return pages;
    }


    /**
     * 查询列表(分页)
     * @param page 分页对象
     * @param line 线路
     * @return
     */
    @ApiOperation(value = "样例列表",httpMethod = "GET", notes = "暂无")
    @GetMapping("findAllByLineOrStartroadOrEndRoad")
    public Response<Page<TrafficLinePage>> list3(@ApiParam(required = true, name = "page", value = "分页对象") @PageableDefault Pageable page,
                                                @ApiParam(required = false, name = "name", value = "姓名") @RequestParam(required = false) String line,@RequestParam(required = false) String startroad,@RequestParam(required = false) String endroad) {

        Page<TrafficLine> pages = service.findAllByLineOrStartroadOrEndroad(line,startroad,endroad,page);
        Page<TrafficLinePage> respS = pages.map(new Converter<TrafficLine, TrafficLinePage>() {
            public TrafficLinePage convert(TrafficLine source) {
                TrafficLinePage resp = new TrafficLinePage();
                if (source != null) {
                    BeanUtils.copyProperties(source, resp);
                }
                return resp;
            }
        });
        return new Response<>(respS);
    }

    /**
     * 查询列表(分页)
     * @param page 分页对象
     * @param line 线路
     * @return
     */
    @ApiOperation(value = "样例列表",httpMethod = "GET", notes = "暂无")
    @GetMapping("findAllByStartroadAndEndroad")
    public Response<Page<TrafficLinePage>> list5(@ApiParam(required = true, name = "page", value = "分页对象") @PageableDefault Pageable page,
                                                 @ApiParam(required = false, name = "name", value = "姓名") @RequestParam(required = false) String line,@RequestParam(required = false) String startroad,@RequestParam(required = false) String endroad) {

        Page<TrafficLine> pages = service.findAllByStartroadAndEndroad(startroad,endroad,page);
        Page<TrafficLinePage> respS = pages.map(new Converter<TrafficLine, TrafficLinePage>() {
            public TrafficLinePage convert(TrafficLine source) {
                TrafficLinePage resp = new TrafficLinePage();
                if (source != null) {
                    BeanUtils.copyProperties(source, resp);
                }
                return resp;
            }
        });
        return new Response<>(respS);
    }


    /**
     * 查询列表(分页)
     * @param page 分页对象
     * @param line 线路
     * @return
     */
    @ApiOperation(value = "样例列表",httpMethod = "GET", notes = "暂无")
    @GetMapping("findAllByLine")
    public List<TrafficLine> list4(@RequestParam(required = false) String line) {

        List<TrafficLine> list = service.findTrafficLineByLine(line);
        return list;

    }



}
